package org.come.control;

import org.apache.commons.lang.StringUtils;
import org.come.Frame.QiandaoListJframe;
import org.come.Frame.ZhuFrame;
import org.come.action.FromServerAction;
import org.come.until.FormsManagement;

import java.util.HashSet;
import java.util.Set;

public class QianDaoControl implements FromServerAction {
    @Override
    public void controlMessFromServer(String mes,String type) {
        //system.out.println("mes:" + mes);
        //打开
        if (mes.contains("open")) {
            FormsManagement.showForm(1100);
            LoginQD.init(mes.substring(5));
            //设置登录天
            QiandaoListJframe.getQiandaoListJpanel().setDay(LoginQD.getEd().size());
            QiandaoListJframe.getQiandaoListJpanel().setDays(LoginQD.getEd());
            //设置领取奖品
            QiandaoListJframe.getQiandaoListJpanel().setLqs(LoginQD.getLq());
        }
        //签到
        if (mes.contains("qd")){
            String[] openInfo = mes.split("=");
            if (openInfo[1].contains("succ")){
                QiandaoListJframe.getQiandaoListJpanel().getDays().add(Integer.valueOf(openInfo[1].split("&")[1]));
            }
            if (openInfo[1].contains("fail")){
                ZhuFrame.getZhuJpanel().addPrompt2(openInfo[1].split("&")[1]);
            }
        }
        //领取
        if (mes.contains("select")){
            String[] openInfo = mes.split("=");
            if (openInfo[1].contains("succ")){
                QiandaoListJframe.getQiandaoListJpanel().getLqs().add(Integer.valueOf(openInfo[1].split("&")[1]));
            }
            if (openInfo[1].contains("fail")){
                ZhuFrame.getZhuJpanel().addPrompt2(openInfo[1].split("&")[1]);
            }
        }
    }
    public static class LoginQD
    {
        private static Set<Integer> ed=new HashSet<>();

        private static Set<Integer> lq=new HashSet<>();

        public static void init(String info)
        {
            if (StringUtils.isEmpty(info))
            {
                return;
            }
            String[] split = info.split("&");
            String[] eds = split[0].split("=");

            if (eds.length > 1)
            {
                ed=new HashSet<>();
                for (String s:eds[1].split(",")){
                    ed.add(Integer.valueOf(s));
                }
            }

            String[] lqs = split[1].split("=");
            if (lqs.length > 1)
            {
                lq=new HashSet<>();
                for (String s:lqs[1].split(",")){
                    lq.add(Integer.valueOf(s));
                }
            }
        }


        public static Set<Integer> getEd()
        {
            return ed;
        }

        public static Set<Integer> getLq()
        {
            return lq;
        }

    }
}
